
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>优化手段template的原理和使用技巧 · petite-vue源码剖析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="^_^肥仔John">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../2_reactivity_system/0_intro.html" />
    
    
    <link rel="prev" href="4_directives/4_ref.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    《Petite-Vue源码剖析》简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../0_intro/1_reason_for_source_reading.html">
            
                <a href="../0_intro/1_reason_for_source_reading.html">
            
                    
                    为什么要读源码
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">在线渲染</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="0_intro.html">
            
                <a href="0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="1_start_from_static_view.html">
            
                <a href="1_start_from_static_view.html">
            
                    
                    从静态视图首次渲染开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="2_initial_rendering_and_rerendering_for_v-if.html">
            
                <a href="2_initial_rendering_and_rerendering_for_v-if.html">
            
                    
                    条件v-if的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="3_initial_rendering_and_rerendering_for_v-for.html">
            
                <a href="3_initial_rendering_and_rerendering_for_v-for.html">
            
                    
                    循环v-for的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="4_directives/1_attribute_binding.html">
            
                <a href="4_directives/1_attribute_binding.html">
            
                    
                    属性绑定v-bind的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="4_directives/2_event_binding.html">
            
                <a href="4_directives/2_event_binding.html">
            
                    
                    事件绑定v-on的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="4_directives/3_bidirectional_binding.html">
            
                <a href="4_directives/3_bidirectional_binding.html">
            
                    
                    双向绑定v-model的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="4_directives/4_ref.html">
            
                <a href="4_directives/4_ref.html">
            
                    
                    特殊指令ref的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.9" data-path="5_tips_for_template.html">
            
                <a href="5_tips_for_template.html">
            
                    
                    优化手段template的原理和使用技巧
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">响应式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../2_reactivity_system/0_intro.html">
            
                <a href="../2_reactivity_system/0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../2_reactivity_system/1_reactive_object_array.html">
            
                <a href="../2_reactivity_system/1_reactive_object_array.html">
            
                    
                    逐行解读reactive如何构建对象和数组的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../2_reactivity_system/2_reactive_map_set.html">
            
                <a href="../2_reactivity_system/2_reactive_map_set.html">
            
                    
                    逐行解读reactive如何构建Map和Set的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../2_reactivity_system/3_register_side_effect.html">
            
                <a href="../2_reactivity_system/3_register_side_effect.html">
            
                    
                    逐行解读effect如何注册副作用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../2_reactivity_system/4_cleanup_algorithm.html">
            
                <a href="../2_reactivity_system/4_cleanup_algorithm.html">
            
                    
                    依赖清理优化算法详解-算法思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../2_reactivity_system/5_smi.html">
            
                <a href="../2_reactivity_system/5_smi.html">
            
                    
                    依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../2_reactivity_system/6_trigger_and_scheduler.html">
            
                <a href="../2_reactivity_system/6_trigger_and_scheduler.html">
            
                    
                    逐行解读trigger和调度器
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">沙箱模型</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../3_sandbox.html">
            
                <a href="../3_sandbox.html">
            
                    
                    模板表达式的沙箱模型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >优化手段template的原理和使用技巧</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x4F18;&#x5316;&#x624B;&#x6BB5;template&#x7684;&#x539F;&#x7406;&#x548C;&#x4F7F;&#x7528;&#x6280;&#x5DE7;"><b>1. </b>&#x4F18;&#x5316;&#x624B;&#x6BB5;template&#x7684;&#x539F;&#x7406;&#x548C;&#x4F7F;&#x7528;&#x6280;&#x5DE7;</a></li><ul><li><span class="title-icon "></span><a href="#&#x4EC0;&#x4E48;&#x662F;template&#x5143;&#x7D20;&#xFF1F;"><b>1.1. </b>&#x4EC0;&#x4E48;&#x662F;template&#x5143;&#x7D20;&#xFF1F;</a></li><li><span class="title-icon "></span><a href="#v-if&#x642D;&#x914D;template"><b>1.2. </b>v-if&#x642D;&#x914D;template</a></li><ul><li><span class="title-icon "></span><a href="#&#x672C;&#x8282;&#x5C0F;&#x7ED3;"><b>1.2.1. </b>&#x672C;&#x8282;&#x5C0F;&#x7ED3;</a></li></ul><li><span class="title-icon "></span><a href="#&#x9519;&#x8BEF;&#x4F7F;&#x7528;"><b>1.3. </b>&#x9519;&#x8BEF;&#x4F7F;&#x7528;</a></li><li><span class="title-icon "></span><a href="#&#x5C0F;&#x7ED3;"><b>1.4. </b>&#x5C0F;&#x7ED3;</a></li></ul></ul></div><h1 id="&#x4F18;&#x5316;&#x624B;&#x6BB5;template&#x7684;&#x539F;&#x7406;&#x548C;&#x4F7F;&#x7528;&#x6280;&#x5DE7;"><a name="&#x4F18;&#x5316;&#x624B;&#x6BB5;template&#x7684;&#x539F;&#x7406;&#x548C;&#x4F7F;&#x7528;&#x6280;&#x5DE7;" class="anchor-navigation-ex-anchor" href="#&#x4F18;&#x5316;&#x624B;&#x6BB5;template&#x7684;&#x539F;&#x7406;&#x548C;&#x4F7F;&#x7528;&#x6280;&#x5DE7;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x4F18;&#x5316;&#x624B;&#x6BB5;template&#x7684;&#x539F;&#x7406;&#x548C;&#x4F7F;&#x7528;&#x6280;&#x5DE7;</h1>
<h2 id="&#x4EC0;&#x4E48;&#x662F;template&#x5143;&#x7D20;&#xFF1F;"><a name="&#x4EC0;&#x4E48;&#x662F;template&#x5143;&#x7D20;&#xFF1F;" class="anchor-navigation-ex-anchor" href="#&#x4EC0;&#x4E48;&#x662F;template&#x5143;&#x7D20;&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x4EC0;&#x4E48;&#x662F;template&#x5143;&#x7D20;&#xFF1F;</h2>
<p>template&#x5143;&#x7D20;&#x662F;2013&#x5E74;&#x5B9A;&#x7A3F;&#x7528;&#x4E8E;&#x63D0;&#x4F9B;&#x4E00;&#x79CD;&#x66F4;&#x7EDF;&#x4E00;&#x3001;&#x529F;&#x80FD;&#x66F4;&#x5F3A;&#x5927;&#x7684;&#x6A21;&#x677F;&#x672C;&#x5B58;&#x653E;&#x65B9;&#x5F0F;&#x3002;&#x5177;&#x4F53;&#x8868;&#x73B0;&#x4E3A;</p>
<ol>
<li><p>&#x901A;&#x8FC7;template&#x5143;&#x7D20;&#x5C5E;&#x6027;<code>content</code>&#x83B7;&#x53D6;&#x5DF2;&#x5B9E;&#x4F8B;&#x5316;&#x7684;HTML&#x5143;&#x7D20;(&#x4E0D;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x800C;&#x5DF2;)</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;tpl&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>a<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>b<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
  <span class="hljs-keyword">const</span> tpl = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;tpl&apos;</span>)
  tpl.content <span class="hljs-comment">// document-fragment</span>
  tpl.content.children[<span class="hljs-number">0</span>].outerHTML <span class="hljs-comment">// &lt;div&gt;a&lt;/div&gt;</span>
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
</li>
<li><p><code>&lt;template&gt;</code>&#x4EE5;&#x53CA;&#x5176;&#x5B50;&#x8282;&#x70B9;&#x5747;&#x4E0D;&#x53EF;&#x89C6;</p>
</li>
<li><code>&lt;template&gt;</code>&#x4E0B;&#x7684;<em>img</em>&#x5143;&#x7D20;&#x7684;<code>src</code>&#x5C5E;&#x6027;&#x5373;&#x4F7F;&#x6709;&#x503C;&#x4E5F;&#x4E0D;&#x4F1A;&#x53D1;&#x51FA;&#x8D44;&#x6E90;&#x8BF7;&#x6C42;</li>
<li><code>&lt;template&gt;</code>&#x4E0B;&#x7684;script&#x548C;css&#x89C4;&#x5219;&#x5747;&#x4E0D;&#x4F1A;&#x89E3;&#x6790;&#x548C;&#x6267;&#x884C;</li>
</ol>
<p>&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x8BF7;&#x67E5;&#x770B;&#xFF1A;<a href="https://www.cnblogs.com/fsjohnhuang/p/4175711.html" target="_blank">&#x300A;HTML&#x8BED;&#x4E49;&#x5316;&#xFF1A;HTML5&#x65B0;&#x6807;&#x7B7E;&#x2014;&#x2014;template&#x300B;</a></p>
<h2 id="v-if&#x642D;&#x914D;template"><a name="v-if&#x642D;&#x914D;template" class="anchor-navigation-ex-anchor" href="#v-if&#x642D;&#x914D;template"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. v-if&#x642D;&#x914D;template</h2>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="handlebars"><span class="xml">
  import { createApp } from &apos;https://unpkg.com/petite-vue?module&apos;

  createApp({
    App: {
      $template: `
      <span class="hljs-tag">&lt;<span class="hljs-name">template</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;status === &apos;offline&apos;&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span> OFFLINE <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">template</span> <span class="hljs-attr">v-else</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span> ONLINE <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
      `,
    }
    status: &apos;online&apos;
  }).mount(&apos;[v-scope]&apos;)
</span></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x8FC7;&#x7A0B;&#x5982;&#x4E0B;&#xFF1A;</p>
<ol>
<li><p>&#x5C06;&#x901A;&#x8FC7;<em>walk.ts</em>&#x4E2D;&#x7684;<code>resolveTemplate</code>&#x65B9;&#x6CD5;&#x5C06;<code>App.$template</code>&#x6E32;&#x67D3;&#x5230;DOM&#x6811;&#x4E0A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">template</span> <span class="hljs-attr">v-if</span>=<span class="hljs-string">&quot;status === &apos;offline&apos;&quot;</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span> OFFLINE <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">template</span> <span class="hljs-attr">v-else</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span> ONLINE <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
</li>
<li><p>&#x89E3;&#x6790;&#x5B50;&#x8282;&#x70B9;<code>&lt;template v-if=&quot;status === &apos;offline&apos;&quot;&gt;&lt;/template&gt;</code></p>
<ol>
<li>&#x8FDB;&#x5165;<em>directives/if.ts</em>&#x8BC6;&#x522B;&#x9644;&#x7740;<code>v-if</code>&#x3001;<code>v-else</code>&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x5E76;&#x5C06;&#x5B83;&#x4EEC;&#x4ECE;DOM&#x6811;&#x4E2D;&#x79FB;&#x9664;</li>
<li>&#x6839;&#x636E;&#x6761;&#x4EF6;&#x8868;&#x8FBE;&#x5F0F;<code>status === &apos;offline&apos;</code>&#x5BF9;&#x4EE5;&#x79BB;&#x7EBF;&#x8282;&#x70B9;(Detached Node)<code>&lt;template v-else&gt;&lt;/template&gt;</code>&#x4E3A;&#x57FA;&#x7840;&#x521B;&#x5EFA;&#x5757;&#x5BF9;&#x8C61;(Block)</li>
</ol>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
</li>
<li><p>&#x5728;&#x5757;&#x5BF9;&#x8C61;&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x4F1A;&#x8BC6;&#x522B;template&#x5143;&#x7D20;&#xFF0C;&#x5E76;&#x901A;&#x8FC7;<code>content.cloneNode</code>&#x65B9;&#x6CD5;&#x590D;&#x5236;<code>&lt;template&gt;</code>&#x7684;&#x5B50;&#x8282;&#x70B9;&#x4F5C;&#x4E3A;&#x6A21;&#x677F;&#xFF0C;&#x8FDB;&#x884C;&#x540E;&#x7EED;&#x89E3;&#x6790;&#x5904;&#x7406;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
</li>
<li><p>&#x6700;&#x540E;<em>directives/if.ts</em>&#x91CC;&#x4F1A;&#x5C06;&#x5757;&#x5BF9;&#x8C61;&#x63D2;&#x5165;&#x7236;&#x8282;&#x70B9;&#x4E2D;&#x4E14;&#x4F4D;&#x4E8E;&#x951A;&#x70B9;&#x5143;&#x7D20;&#x524D;&#x9762;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span> ONLINE <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
</li>
</ol>
<h3 id="&#x672C;&#x8282;&#x5C0F;&#x7ED3;"><a name="&#x672C;&#x8282;&#x5C0F;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x672C;&#x8282;&#x5C0F;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.1. &#x672C;&#x8282;&#x5C0F;&#x7ED3;</h3>
<ol>
<li>&#x8FD9;&#x91CC;&#x5229;&#x7528;&#x7684;&#x662F;<code>&lt;template&gt;</code>&#x5143;&#x7D20;&#x672C;&#x8EAB;&#x7684;&#x7279;&#x6027;&#x5B9E;&#x73B0;&#x5728;&#x7EBF;&#x89E3;&#x6790;&#x7528;&#x6237;&#x4E0D;&#x53EF;&#x89C1;(&#x4F60;&#x770B;&#x4E0D;&#x5230;&#x6211;&#xFF0C;&#x4F60;&#x770B;&#x4E0D;&#x5230;&#x6211;:D)&#xFF0C;&#x548C;&#x907F;&#x514D;&#x5982;<code>&lt;img src=&quot;logo.png&quot;&gt;</code>&#x53D1;&#x9001;&#x65E0;&#x6548;&#x8BF7;&#x6C42;&#x7684;&#x95EE;&#x9898;&#xFF1B;</li>
<li>&#x7531;&#x4E8E;<code>&lt;template&gt;</code>&#x662F;&#x5728;<em>block.ts</em>&#x4E2D;&#x5904;&#x7406;&#x83B7;&#x53D6;&#x6A21;&#x677F;&#xFF0C;&#x56E0;&#x6B64;<code>v-for</code>&#x642D;&#x914D;<code>&lt;template&gt;</code>&#x7684;&#x539F;&#x7406;&#x548C;<code>v-if</code>&#x662F;&#x4E00;&#x81F4;&#x7684;&#x3002;</li>
</ol>
<h2 id="&#x9519;&#x8BEF;&#x4F7F;&#x7528;"><a name="&#x9519;&#x8BEF;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x9519;&#x8BEF;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x9519;&#x8BEF;&#x4F7F;&#x7528;</h2>
<p>&#x867D;&#x7136;template&#x5143;&#x7D20;&#x80FD;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x4F18;&#x5316;&#x7528;&#x6237;&#x4F53;&#x9A8C;&#x548C;&#x6027;&#x80FD;&#xFF0C;&#x4F46;&#x6709;&#x4E9B;&#x65F6;&#x5019;&#x4E5F;&#x4F1A;&#x8BA9;&#x6211;&#x4EEC;&#x6389;&#x5230;&#x5751;&#x91CC;&#x9762;&#xFF0C;&#x4E0B;&#x9762;&#x4E00;&#x8D77;&#x7ED5;&#x8FC7;&#x8FD9;&#x4E9B;&#x5751;&#x5427;&#xFF01;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="javascript">
  <span class="hljs-keyword">import</span> { createApp } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;https://unpkg.com/petite-vue?module&apos;</span>

  createApp({
    App: {
      $template: <span class="hljs-string">`
      &lt;template&gt;
        &lt;div&gt;Hello&lt;/div&gt;
      &lt;/template&gt;
      `</span>,
    }
    status: <span class="hljs-string">&apos;online&apos;</span>
  }).mount(<span class="hljs-string">&apos;[v-scope]&apos;</span>)
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>&#x6839;&#x5757;&#x5BF9;&#x8C61;&#x5BF9;&#x5E94;&#x7684;&#x662F;<code>&lt;div v-scope=&quot;App&quot;&gt;&lt;/div&gt;</code>&#xFF0C;&#x800C;<code>&lt;template&gt;</code>&#x7531;&#x4E8E;&#x6CA1;&#x6709;&#x9644;&#x52A0;<code>v-if</code>&#x6216;<code>v-for</code>&#xFF0C;&#x56E0;&#x6B64;&#x4E0D;&#x4F1A;&#x4E3A;&#x5176;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x5757;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x6700;&#x540E;&#x5F97;&#x5230;&#x7684;UI&#x5C31;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Hello<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>&#x7528;&#x4E8E;&#x65E0;&#x6CD5;&#x770B;&#x5230;&#x6587;&#x5B57;Hello&#x3002;</p>
<h2 id="&#x5C0F;&#x7ED3;"><a name="&#x5C0F;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x5C0F;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.4. &#x5C0F;&#x7ED3;</h2>
<p>&#x901A;&#x8FC7;&#x672C;&#x7BC7;&#x5185;&#x5BB9;&#x7684;&#x4ECB;&#x7ECD;&#xFF0C;&#x6211;&#x4EEC;&#x8BB0;&#x5F97;<code>&lt;template&gt;</code>&#x5FC5;&#x987B;&#x642D;&#x914D;<code>v-if</code>&#x6216;<code>v-for</code>&#x4F7F;&#x7528;&#x54E6;&#xFF01;</p>
<script type="text/javascript">
  /**
   * Donation
   */
  function showQR(){
    var rewardButton = document.getElementById('rewardButton')
    var qr = document.getElementById('QR')
    if (rewardButton) {
      rewardButton.style.display = 'none'
    }
    if (qr) {
      qr.style.display = 'block'
    }
  }
  setInterval(showQR, 1000)

  /**
   * Baidu analysis
   */
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8bca1e3aa362a7f85dbe8304cff65225";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
</script>


<footer class="page-footer"><span class="copyright">Copyright &#xA9; fsjohnhuang 2022 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2022-04-26 09:46:36
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="4_directives/4_ref.html" class="navigation navigation-prev " aria-label="Previous page: 特殊指令ref的工作原理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../2_reactivity_system/0_intro.html" class="navigation navigation-next " aria-label="Next page: 章节介绍">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"优化手段template的原理和使用技巧","level":"2.9","depth":1,"next":{"title":"章节介绍","level":"3.1","depth":1,"path":"2_reactivity_system/0_intro.md","ref":"2_reactivity_system/0_intro.md","articles":[]},"previous":{"title":"特殊指令ref的工作原理","level":"2.8","depth":1,"path":"1_online_rendering/4_directives/4_ref.md","ref":"1_online_rendering/4_directives/4_ref.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","theme-comscore","search-pro","code","back-to-top-button","tbfed-pagefooter","chapter-fold","expandable-chapters","splitter","anchor-navigation-ex","donate","sharing-plus","baidu"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy fsjohnhuang 2022"},"chapter-fold":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赞赏","title":"","wechat":"/petite-vue-source-reading/images/wechat.jpg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":true},"theme-comscore":{},"back-to-top-button":{},"baidu":{"token":"8bca1e3aa362a7f85dbe8304cff65225"},"sharing":{"qq":false,"all":["qq","qzone","weibo","whatsapp","twitter","facebook","line"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"^_^肥仔John","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"petite-vue源码剖析","gitbook":"*","description":""},"file":{"path":"1_online_rendering/5_tips_for_template.md","mtime":"2022-04-26T01:46:36.984Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-05T01:20:11.998Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

